<?php
namespace app\index\controller;

use think\Db;
use app\index\model\Job;
use app\common\controller\IndexBase;


class Company extends IndexBase
{
    public function lists()
    {   
        if (input('post.')) {
            $name = input('post.name');
            if ($name) {
                session('gs_name',$name);
                $where['name']  = ['like','%'.$name.'%'];
            }
        }
        $where['is_show']  = 1;
        $res = db('company')->where($where)->field('id,name,img_url,is_index,add_time')->order('is_index desc,sort desc,add_time desc,id desc')->select();
        // var_dump($res);exit();
        return $this->fetch('lists',['list'=>$res]);
    }
    public function index()
    {   
        $id = input('get.id');
        $res = db('company')->where(['id'=>$id,'is_show'=>1])->find();
        if ($res) {
            $gz_zw_list = db('users')->where('openid',session('wx'))->value('gs');
            $gz_zw_arr = explode(',', $gz_zw_list);
            if (in_array($id, $gz_zw_arr)) {
                $zw_gz = 1;
            }else{
                $zw_gz = 0;
            }
            $this->assign('zw_gz',$zw_gz);
            $this->assign('co_data',$res);
            return $this->fetch('index');
        }else{
            echo "<script language='javascript' type='text/javascript'> alert('该公司已下线');location.href='javascript:history.back(-1)'; </script>"; 
        }
    }
    public function zw_list()
    {   
        
        if (input('post.name')) {
            $id = input('post.id');
            session('getid',$id);
            $name = input('post.name');
            if ($name) {
                session('gs_zw_se_name',$name);
                $where['jobname']  = ['like','%'.$name.'%'];
            }
        }
        if ($_GET['m']) {
            //选择条件类型
            $m = input('get.m');
            //选中条件
            $n = input('get.n');
            $id = input('get.cid');
            if ($m == 1) {
                $where['city']  = $n;
            }elseif ($m == 2) {
                $where['wage']  = $n;
            }elseif ($m == 3) {
                $where['worktime']  = $n;
            }else{
                $where['type']  = $n-1;
            }
        }
        if (session('getid')) {
            $id = session('getid');
        }
        if (input('get.id')) {
            $id = input('get.id');
        }
        
        // var_dump($id);die();
        // var_dump($where);die;
        //工作地点
        $city_list = db('job')->where(array('cid'=>$id))->column('city');
        //月薪
        $wage_list = db('job')->where(array('cid'=>$id))->column('wage');
        //工作时间
        $worktime_list = db('job')->where(array('cid'=>$id))->column('worktime');
        $city_list = array_unique($city_list);
        $wage_list = array_unique($wage_list);
        $worktime_list = array_unique($worktime_list);
        $where['is_show']  = 1;
        $where['cid'] = $id;
        $res_co = db('company')->field('id,name,img_url')->where(['id'=>$id,'is_show'=>1])->find();
        $res = model('job')->where($where)->field('jobid,cid,jobname,wage,worktime,type,city,is_index,update_time')->order('is_index desc,sort desc,update_time desc,add_time desc,jobid desc')->select();
        return $this->fetch('zw_list',['list'=>$res,'co_data'=>$res_co,'city_list'=>$city_list,'wage_list'=>$wage_list,'worktime_list'=>$worktime_list]);
    }
    public function gs_gz()
    {   

        $id = input('post.id');

        $openid = session('wx');
        $gz_zw_list = db('users')->where('openid',$openid)->value('gs');
        $gz_zw_arr = explode(',', $gz_zw_list);
        if (in_array($id, $gz_zw_arr)) {
            foreach ($gz_zw_arr as $key=>$value){  
                if ($value === $id)
                    unset($gz_zw_arr[$key]);  
            }
            $gz_type= 1;  
        }else{
            array_push($gz_zw_arr,$id);
            $gz_type= 0;  
        }

        $data['gs'] = implode(',', $gz_zw_arr);
        $res = db('users')->where('openid',$openid)->update($data);
        if ($res) {
            if ($gz_type) {
                return json(['code'=>1,'msg'=>'取消关注']);
            }else{
                return json(['code'=>2,'msg'=>'关注成功']);
            }
            
        }else{
            return json(['code'=>0,'msg'=>'操作错误']);
        }
    }
    public function gz_list()
    {
        $openid = session('wx');
        $gz_zw_list = db('users')->where('openid',$openid)->value('gs');
        $gz_zw_arr = explode(',', $gz_zw_list);
            
        $res = db('company')->where('id','in',$gz_zw_arr)->field('id,name,is_index,add_time,is_show,is_index')->order('is_index desc,sort desc,add_time desc,id desc')->select();
        // var_dump($res);exit();
        if ($res) {
            return $this->fetch('gz_list',['list'=>$res]);
        }else{
            return $this->fetch('gz_list',['list'=>'']);
        }
        
    
    }
}